﻿using System;
using System.IO.Ports;
using System.Threading;
using System.Windows;
using static WriteCode.MainWindow;

namespace WriteCode.IngenuBorder
{
    public class Ingenu
    {
        private protected SerialPort IngenuSerialport = new SerialPort();
        public Ingenu(ref string err_info, string portName, int baudRate, Parity parity)
        {
            try
            {
                IngenuSerialport = new SerialPort(); // 初始化串口对象
                IngenuSerialport.PortName = portName; // 设置串口名称
                IngenuSerialport.BaudRate = baudRate; // 设置波特率
                IngenuSerialport.Parity = parity; // 设置校验位
                IngenuSerialport.DataBits = 8; // 设置数据位
                IngenuSerialport.StopBits = StopBits.One; // 设置停止位
                IngenuSerialport.Open();// 打开串口
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error opening serial port: " + ex.Message);// 处理异常
            }
        }
        /// <summary>
        /// 串口数据发送
        /// </summary>
        /// <param name="send_data"></param>
        /// <param name="errinfo"></param>
        public void SendData(byte[] send_data, ref string errinfo)
        {
            try
            {
                if (!IngenuSerialport.IsOpen)
                {
                    IngenuSerialport.Open();
                }
                if (IngenuSerialport.PortName != null && IngenuSerialport.IsOpen)
                {
                    IngenuSerialport.ReadExisting();                          //发送前清缓冲区
                    IngenuSerialport.Write(send_data, 0, send_data.Length);
                }
                else
                {
                    errinfo = "串口未打开，无法发送数据";
                }
            }
            catch (Exception ex)
            {
                errinfo = "发送数据出现错误";
            }
        }
        /// <summary>
        /// 等待串口数据接收
        /// </summary>
        /// <param name="receive_data"></param>
        /// <param name="wait_timer"></param>
        /// <param name="setting_lenth"></param>
        public byte[] Receive_Wait(int wait_timer, int setting_lenth,out string err_info)
        {
            err_info = string.Empty;
            int data_lenth = 0;
            byte[] receive_data = new byte[setting_lenth];
            try
            {
                for (int b = 0; b < wait_timer; b++)   //最长等待500ms
                {
                    Thread.Sleep(10);                  //等待10ms
                    data_lenth = IngenuSerialport.BytesToRead;   //获取接收数据长度
                    if (data_lenth >= setting_lenth) break;       //避开接收异常，如上电断电引起的0X00
                }
                if (data_lenth == setting_lenth)
                {
                    Array.Resize(ref receive_data, data_lenth);               //重定义数组长度
                    IngenuSerialport.Read(receive_data, 0, data_lenth);      //读取数据
                }
                else
                {
                    err_info = "等待数据接收超时";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error reading data: {ex.Message}");
            }
            return receive_data;
        }

        /// <summary>
        /// 编写工装板地址
        /// </summary>
        /// <param name="address">输入地址</param>
        /// <returns></returns>
        public byte[] Display_vWriteAddressInfo(byte address)
        {
            byte[] send_cmd = new byte[8];
            send_cmd[0] = (byte)AddressCmd_Enum.Header;
            send_cmd[1] = (byte)AddressCmd_Enum.Address;
            send_cmd[2] = (byte)AddressCmd_Enum.WriteCommand;
            send_cmd[3] = (byte)AddressCmd_Enum.WriteFcommand;
            send_cmd[4] = 1;
            send_cmd[5] = address;
            send_cmd[6] = 0x00;
            send_cmd[7] = (byte)AddressCmd_Enum.Last;
            return send_cmd;
        }
        /// <summary>
        /// 读取工装板地址填充
        /// </summary>
        /// <param name="err_info"></param>
        /// <returns></returns>
        public byte[] Display_vQureyAddressInfo()
        {
            byte[] send_cmd = new byte[8];
            send_cmd[0] = (byte)AddressCmd_Enum.Header;
            send_cmd[1] = (byte)AddressCmd_Enum.Address;
            send_cmd[2] = (byte)AddressCmd_Enum.ReadCommand;
            send_cmd[3] = (byte)AddressCmd_Enum.ReadFcommand;
            send_cmd[4] = 1;
            send_cmd[5] = 0x00;
            send_cmd[6] = 0x00;
            send_cmd[7] = (byte)AddressCmd_Enum.Last;
            return send_cmd;
        }
        /// <summary>
        /// 读取工装板的ADC
        /// </summary>
        /// <param name="address">工装板地址</param>
        /// <returns></returns>
        public byte[] Display_vReadADC(byte address)
        {
            byte[] send_cmd = new byte[8];
            send_cmd[0] = (byte)AddressCmd_Enum.Header;
            send_cmd[1] = address;
            send_cmd[2] = (byte)AddressCmd_Enum.ADCCommand;
            send_cmd[3] = (byte)(~AddressCmd_Enum.ADCCommand);
            send_cmd[4] = 1;
            send_cmd[5] = 0x00;
            send_cmd[6] = 0x00;
            send_cmd[7] = (byte)AddressCmd_Enum.Last;
            return send_cmd;
        }
    }
}
